import { Component } from "vue-facing-decorator";
import { UserService } from '@/service/UserService';
import LoggerUtil from '@/utils/LoggerUtil';
import Util from '@/utils/Util';
import UniUtil from '@/utils/UniUtil';
import { CustField } from '@/constant/CustField';
import Constant, { FilePrefix, TopBar } from '@/constant/Constants';
import QiniuService from "@/service/QiniuService";
import { UserInfo } from "@/model/UserInfo";
import { MyVue } from "@/common/MyVue";
import User from "../tabbar/user/user";
@Component
export default class Login extends MyVue {
    showBindPhoneModal: boolean = false;
    appName = Constant.appName;
    user = new UserInfo.User();
    cdnHost = Constant.cdnHost;
    edit: boolean = false;
    async onLoad() {
        uni.setNavigationBarTitle({ title: TopBar.LOGIN });
        let user = UserService.getUser();
        if (user && !Util.isEmpty(user.head)) {
            Util.copy(this.user, user);
        }
        LoggerUtil.info("Login onLoad user: {}", JSON.stringify(this.user));
        let params = Util.getPageParams();
        LoggerUtil.info("Login onLoad params: {}", JSON.stringify(params));
        if (params && params.from === "edit") {
            this.edit = true;
        }
    }

    async loadPost() {

    }

    onSwitchSex(e) {
        this.user.sex = e.detail.value ? 1 : 2;
    }
    doLogin(e) {
        LoggerUtil.info("doLogin {}", JSON.stringify(this.user));
        if (Util.isEmpty(this.user.head)) {
            UniUtil.showError({ title: "头像为空" });
            return;
        }
        if (Util.isEmpty(this.user.nick)) {
            UniUtil.showError({ title: "昵称为空" });
            return;
        }

        let referUid = UniUtil.getGlobalData(CustField.AUTH_FROM);
        this.user.refUid = referUid;
        let that = this;
        UserService.login(this.user, function (user: UserInfo.User) {

            UniUtil.delGlobalData(CustField.AUTH_FROM);
            if (!that.edit) {
                UniUtil.navigateBack();
            }
        });
    }

    onChooseAvatar(e) {

        if (Util.isEmpty(e.detail) || Util.isEmpty(e.detail.avatarUrl)) {
            return;
        }
        let head = e.detail.avatarUrl;
        let key = FilePrefix.WX_AVATAR + new Date().getTime() + '.png';
        let that = this;
        QiniuService.upload(head, key, function () {
            that.user.head = key;
        });
    }

    showModal() {
        this.showBindPhoneModal = true;
    }
    hideModal() {
        this.showBindPhoneModal = false;
    }
    async getPhoneNumber(e) {
        if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
            return;
        }
        LoggerUtil.info(JSON.stringify(e));
        let that = this;
        that.hideModal();
        let req = new EncryptedRequest();
        req.setEncryptedData(e.detail.encryptedData);
        req.setIvData(e.detail.iv);
        let res = await UserService.getPhone(req, function () {
            UniUtil.delGlobalData(CustField.AUTH_FROM);
            UniUtil.navigateBack();
        });
        this.user.phone = res.getPhoneNumber();
    }

    onNickBlur(e) {
        this.user.nick = e.detail.value || e.target.value;
    }

    sexChange(e) {
        LoggerUtil.info("sexChange {}", e.detail.value);
        this.user.sex = e.detail.value ? 1 : 2;
    }

} 